package com.example.jiaju.ui.main;

import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import com.example.jiaju.DataApplication;
import com.example.jiaju.R;
import com.example.jiaju.adapter.NewHouseAdapter;
import com.example.jiaju.api.HouseApi;
import com.example.jiaju.databinding.ActivityHouseBinding;
import com.example.jiaju.entity.House;
import com.example.jiaju.utils.MyItemDecoration;
import com.example.jiaju.utils.RetrofitUtils;
import com.gyf.immersionbar.ImmersionBar;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class HouseActivity extends AppCompatActivity {


    private String TAG="HouseActivity";

    private RecyclerView houseRecyclerView;
    private NewHouseAdapter newHouseAdapter;

    private List<House.PageDTO.ListDTO> list=new ArrayList<>();
    private ActivityHouseBinding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding= DataBindingUtil.setContentView(this,R.layout.activity_house);

        //设置黑色状态栏字体以及重叠问题
        ImmersionBar.with(this).statusBarDarkFont(true).fitsSystemWindows(true).init();

        //网络请求三步走
        //1.获取到API接口
        HouseApi houseApi= RetrofitUtils.getRetrofit(DataApplication.HTTP_URL).create(HouseApi.class);
        //2.获取到实体类
        Call<House> call=houseApi.getAllHouse(1,5);
        //3.消息请求队列，取出数据
        call.enqueue(new Callback<House>() {
            @Override
            public void onResponse(Call<House> call, Response<House> response) {
                House house=response.body();
                if(house==null){
                    Toast.makeText(HouseActivity.this,"楼盘数据加载失败！",Toast.LENGTH_SHORT).show();
                }else{
                    list=house.page.list;
                    Log.d(TAG, "onResponse: "+list);
                    houseRecyclerView=findViewById(R.id.house_recyclerview);
                    newHouseAdapter=new NewHouseAdapter(HouseActivity.this,list);
                    houseRecyclerView.setAdapter(newHouseAdapter);
                    houseRecyclerView.addItemDecoration(new MyItemDecoration(8));
                    houseRecyclerView.setLayoutManager(new LinearLayoutManager(HouseActivity.this,LinearLayoutManager.VERTICAL,false));

                }
            }

            @Override
            public void onFailure(Call<House> call, Throwable t) {

            }
        });
        //回到HomeActivity
        binding.include.returnBtn.setOnClickListener(v->finish());

    }
}